package com.manger.frame;

import com.alibaba.fastjson.JSONObject;
import com.manger.theme.CustomFLatLightLaf;
import com.manger.utils.DataTools;
import com.manger.utils.RemoteExecute;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * @ClassName LoginFrame
 * @Description TODO
 * @Author @LiuHongJun
 * @Date 2025/9/15 19:32
 * @Version 1.0
 */
public class LoginFrame {


    public static void init() {

        JFrame frame = new JFrame("登录");
        frame.setSize(280, 220);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 15));
       // frame.setLayout(new GridLayout(4, 2,));

// 创建组件
        JLabel userLabel = new JLabel("姓名:");
        JTextField userField = new JTextField(18);
        JLabel passLabel = new JLabel("密码:");
        JPasswordField passField = new JPasswordField(18);
        JLabel selectLabel = new JLabel("代理:");
        String[] items = {"使用代理", "不使用代理"};
        JComboBox<String> comboBox = new JComboBox<>(items);
        comboBox.setSelectedIndex(0); // 默认选择“选项2”
        comboBox.setPreferredSize(new Dimension(200, comboBox.getPreferredSize().height));

        JButton loginButton = new JButton("登录");
        JButton resetButton = new JButton("重置");

// 添加组件
        frame.add(userLabel);
        frame.add(userField);
        frame.add(passLabel);
        frame.add(passField);
        frame.add(selectLabel);
        frame.add(comboBox);
        frame.add(loginButton);
        frame.add(resetButton);
        frame.setResizable(false);

        frame.setVisible(true);

        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation(dim.width / 2 - frame.getSize().width / 2, dim.height / 2 - frame.getSize().height / 2);

        loginButton.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                try {
                    String userPhone = userField.getText();
                    String smsCode = passField.getText();
                    if ("".equals(userPhone) || "".equals(smsCode)) {
                        JOptionPane.showMessageDialog(null, "请填写数据！", "警告", JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                    String url = DataTools.baseUrl + "/login/pcLogin?userName=" + userPhone + "&password=" + smsCode;
                    JSONObject jsonObject = RemoteExecute.execute(url);
                    if (jsonObject.getIntValue("code") != 1) {
                        JOptionPane.showMessageDialog(null, jsonObject.getString("msg"), "警告", JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                    DataTools.loginUserId = jsonObject.getString("data");
                    frame.dispose();
                    AutoCaseFrame.init();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
        resetButton.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                userField.setText("");
                passField.setText("");
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });

        comboBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    DataTools.hasUseProxy = e.getItem().equals("使用代理");
                }
            }
        });
    }
}
